package com.java.controller;

import com.java.exceptioin.BusinessException;
import com.java.result.ResponseEnum;
import com.java.result.Result;
import com.java.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

@Api("oss文件管理")
@RestController
@RequestMapping("/api/oss/file")
public class OssController {

    @Resource
    private OssService ossService;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result upload(@RequestParam("file")MultipartFile file,
                        @RequestParam("module") String module){
        String url = null;
        try {
            String filename = file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            url = ossService.uploadFile(inputStream,module,filename);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR,e);
        }
        return Result.ok(url);
    }

    @ApiOperation("文件删除")
    @DeleteMapping("/remove")
    public Result remove(@RequestParam("url") String url){
         ossService.remove(url);
         return Result.ok();
    }
}
